package com.timing.finder.framework;

import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author JingGe(* ^ ▽ ^ *)
 * @date 2023-03-07 11:07
 * @email 1158055613@qq.com
 */
@Slf4j
public class DefaultTimeSeriesDataLoader<T extends TimeSeriesData> implements TimeSeriesDataLoader<T> {
    private final DBRepository<T> dbRepository;
    private final TimingFinderConfig config;
    private Map<String, Object> filterParams;

    public DefaultTimeSeriesDataLoader(DBRepository<T> dbRepository, TimingFinderConfig config) {
        this.dbRepository = dbRepository;
        this.config = config;
    }

    public DefaultTimeSeriesDataLoader(DBRepository<T> dbRepository, TimingFinderConfig config, Map<String, Object> filterParams) {
        this.dbRepository = dbRepository;
        this.config = config;
        this.filterParams = filterParams;
    }

    @Override
    public List<T> load(int pageNum) {
        if (config.starID.equals(config.endID)) {
            return Collections.emptyList();
        }
        log.info("本次查询参数：页号：{}，批大小：{}，起始ID：{}，截止ID：{}{}", pageNum, config.batchSize, config.starID, config.endID, config.endID < 0 ? "，注意！当前的终止ID为负数，默认传Long的最大值" : "");
        return dbRepository.query(filterParams, pageNum, config.batchSize, config.starID, config.endID < 0 ? Long.MAX_VALUE : config.endID);
    }

    @Override
    public DBRepository<T> getDBRepository() {
        return dbRepository;
    }
}
